/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cardarus.operatingsystem.struts.forms;

import java.text.DateFormat;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class responds to saving Customer's information.
 * This class maintains Customer's information of Reward plan registration.
 * 
 * @author Daqi
 */
public class RegisterRewardInputForm extends org.apache.struts.action.ActionForm {

    private DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
    private boolean newUser;
    private String pin;
    private int points;
    //SystemUser Info.
    private String userName;
    private String password;
    private String name;
    private String familyName;
    private Date birthday;
    private String phone;
    private String email;
    private String address;
    private String dateString;

    public String getDateString() {
        return dateString;
    }

    public void setDateString(String dateString) {
        this.dateString = dateString;
        try {
            this.birthday = df.parse(dateString);
        } catch (ParseException ex) {
            Logger.getLogger(RegisterRewardInputForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    /**
     * Initial each attributes
     */
    public void clearAll() {
        newUser = true;
        pin = "";
        points = 0;

        userName = "";
        password = "";
        name = "";
        familyName = "";
        phone = "";
        email = "";
        address = "";
        dateString = "";
        birthday = null;
    }

    /**
     * @return the Customer type
     */
    public boolean isNewUser() {
        return newUser;
    }

    /**
     * @param newUser the Customer type to set
     */
    public void setNewUser(boolean newUser) {
        this.newUser = newUser;
    }

    /**
     * @return Customer address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the Customer address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return Customer email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the Customer email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return Customer's family name
     */
    public String getFamilyName() {
        return familyName;
    }

    /**
     * @param familyName the family name to set
     */
    public void setFamilyName(String familyName) {
        this.familyName = familyName;
    }

    /**
     * @return the Customer password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the Customer phone number
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the Customer pin number
     */
    public String getPin() {
        return pin;
    }

    /**
     * 
     * @param pin the pin to set 
     */
    public void setPin(String pin) {
        this.pin = pin;
    }

    /**
     * @return the Customer name
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the username to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the Customer points
     */
    public int getPoints() {
        return points;
    }

    /**
     * 
     * @param points the points to set
     */
    public void setPoints(int points) {
        this.points = points;
    }

    /**
     * @return Customer first name
     */
    public String getName() {
        return name;
    }

    /**
     * @param string the first name to set
     */
    public void setName(String string) {
        name = string;
    }

    /**
     * Default constructor function
     */
    public RegisterRewardInputForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();

        //User name
        if (getUserName() == null || getUserName().length() < 1) {
            errors.add("username", new ActionMessage("error.customer.nullUsername"));
        }
        //Password
        if (getPassword() == null || getPassword().length() < 1) {
            errors.add("password", new ActionMessage("error.customer.nullPassword"));
        }
        //First name
        if (getName() == null || getName().length() < 1) {
            errors.add("name", new ActionMessage("error.customer.nullName"));
        }
        //Last name
        if (getFamilyName() == null || getFamilyName().length() < 1) {
            errors.add("familyname", new ActionMessage("error.customer.nullLastname"));
        }
        //Birthday name
        if (getDateString() == null || getDateString().length() < 1) {
            errors.add("birthday", new ActionMessage("error.customer.nullBirthday"));
        }
        //Phone Number
        if (getPhone() == null || getPhone().length() < 1) {
            errors.add("phone", new ActionMessage("error.customer.nullPhonenumber"));
        }
        //Email
        if (getEmail() == null || getEmail().length() < 1) {
            errors.add("emailNull", new ActionMessage("error.customer.nullEmail"));
        } else if (getEmail().matches("^[a-zA-Z0-9._-]+@([a-zA-Z0-9-_]+\\.)+[a-zA-Z0-9-_]+$") == false){
            errors.add("emailInvaild", new ActionMessage("error.customer.invalid"));
        }
        //Address
        if (getAddress() == null || getAddress().length() < 1) {
            errors.add("address", new ActionMessage("error.customer.nullAddress"));
        }


        return errors;
    }
}
